import { Component, OnInit, ViewChild} from '@angular/core';
import { HttpRequest } from "../../../service/httpRequest";
import { HideElementService } from "../../../service/hideElement.service";
import { PageModel } from "../../../model/page-model";
import { flyIn } from '../../../animations/fly-in';
import { GlobalVariable } from '../../../global.config';
import { ModalDirective } from 'ng2-bootstrap/modal';

@Component({
  selector: 'app-role',
  templateUrl: './role.component.html',
  styleUrls: ['./role.component.css'],
  animations: [
    flyIn
  ]
})
export class RoleComponent implements OnInit {

  elc = {a:'角色添加',e:'角色编辑',d:'角色删除',o:'关联权限'};
  tool = GlobalVariable.tooltip;
  data:any;
  keyword:any;

  @ViewChild('myModal') public myModal:ModalDirective;
  model:any = {name:''};
  buttonType:number;
  disabled:string = "";

  constructor(private httpRequest:HttpRequest,private he:HideElementService) {
    this.elc = this.he.hideElement(this.elc);
  }

  ngOnInit() {
    this.getData();
  }

  getData(){
    this.httpRequest.sendGet('roles/list').subscribe(res=>{
      this.data = res.result;
    },err=>console.log(err));
  }

  setModel(model){
    this.model = JSON.parse(JSON.stringify(model));
  }

  confirmNew():void{

    this.disabled='disabled';
    this.httpRequest.sendPost('roles', {name:this.model.name},'op').subscribe(res=>{
      alert('角色创建成功');
      this.getData();
      this.myModal.hide();
      this.disabled='';
    },err=>{
      alert('角色创建失败');
      this.disabled='';
    });
  }


  confirmEdit():void{
    this.disabled='disabled';
    this.httpRequest.sendPut('roles/'+this.model.id,this.model,'op').subscribe(res=>{
      alert('角色修改成功');
      this.getData();
      this.myModal.hide();
      this.disabled='';
    },err=>{
      alert('角色修改失败');
      this.disabled='';
    });
  }

  //confirmDelete():void{
  //  this.disabled='disabled';
  //  this.httpRequest.sendPost('admin-role/delete',{
  //    id:this.model.id
  //  },'op').subscribe(res=>{
  //
  //      this.myModal.hide();
  //
  //    this.disabled='';
  //  });
  //}
}
